package com.lfg.leetcode.leetcode100;


public class No165compareVersion {

    public static void main(String[] args) {
        int ret = compareVersion("1.0.1", "2");
        System.out.println(ret);
    }

    public static int compareVersion(String version1, String version2) {

        int idx1 = 0;
        int idx2 = 0;

        while (idx1 < version1.length() || idx2 < version2.length()) {
            int v1 = 0;
            int v2 = 0;
            while (idx1 < version1.length()) {
                if (version1.charAt(idx1) == '.') {
                    idx1++;
                    break;
                }
                v1 = v1 * 10 + version1.charAt(idx1++) - '0';
            }

            while (idx2 < version2.length()) {
                if (version2.charAt(idx2) == '.') {
                    idx2++;
                    break;
                }
                v2 = v2 * 10 + version2.charAt(idx2++) - '0';
            }

            if (v1 > v2) {
                return 1;
            } else if (v1 < v2) {
                return -1;
            }
        }

        return 0;
    }

    public static int compareVersion2(String version1, String version2) {
        if (null == version1 && null == version2) {
            return 0;
        }
        if (null == version2) {
            return 1;
        }
        if (null == version1) {
            return -1;
        }
        if (version1.equals(version2)) {
            return 0;
        }
        String[] array1 = version1.split("\\.");
        String[] array2 = version2.split("\\.");
        int length = Math.max(array1.length, array2.length);

        for (int i = 0; i < length; i++) {
            int a = 0;
            if (i < array1.length) {
                a = Integer.valueOf(array1[i]);
            }
            int b = 0;
            if (i < array2.length) {
                b = Integer.valueOf(array2[i]);
            }
            if (a > b) {
                return 1;
            } else if (a < b) {
                return -1;
            }
        }

        return 0;
    }
}
